package com.baihan.angus.app.game.busshow.ui.home.recommend;

import android.os.Bundle;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.baihan.angus.app.game.basic.base.BaseFragment;
import com.baihan.angus.app.game.busshow.BR;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.RecommendAdp;
import com.baihan.angus.app.game.busshow.databinding.FragmentRecommendBinding;
import com.baihan.angus.app.game.common.bean.response.BbsResponse;
import com.baihan.angus.app.game.common.bean.response.Userinfo;

import java.util.HashMap;
import java.util.Map;

/**
 * 推荐
 */
public class RecommendFragment extends BaseFragment<FragmentRecommendBinding, RecommendVM> {

    private RecommendAdp mRecommendAdp;
    private Map<String, Object> mParameter = new HashMap<>();

    private Userinfo userInfo;
    private int mPage = 1;
    private int mSize = 10;
    private int totalPage = 0;

    public RecommendFragment() {
        // Required empty public constructor
    }

    public static RecommendFragment newInstance() {
        RecommendFragment fragment = new RecommendFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getViewModelId() {
        return BR.recommendVM;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_recommend;
    }

    @Override
    protected void initView() {

        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);

        mRecommendAdp = new RecommendAdp();
        mVDBinding.rvRecommend.setLayoutManager(layoutManager);
        mRecommendAdp.bindToRecycleView(mVDBinding.rvRecommend);

        mVDBinding.srlRecommend.setOnRefreshListener(refreshLayout -> {
            mPage = 1;
            mParameter.put("page", mPage);
            mViewModel.getBbsList(mParameter);
        });

        mVDBinding.srlRecommend.setOnLoadMoreListener(refreshLayout -> {
            mParameter.put("page", ++mPage);
            mViewModel.getBbsList(mParameter);
        });

        mRecommendAdp.setOnItemClickListener(new RecommendAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

            }

            @Override
            public void onPraiseClick(int position) {

            }
        });
    }

    @Override
    protected void initData() {
        mParameter.put("page", mPage);
        mParameter.put("size", mSize);
        mViewModel.getBbsList(mParameter);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldBbs.observe(this, new Observer<BbsResponse.DataX>() {
            @Override
            public void onChanged(BbsResponse.DataX dataX) {
                if (mVDBinding.srlRecommend.isRefreshing()) mVDBinding.srlRecommend.finishRefresh();
                if (mVDBinding.srlRecommend.isLoading()) mVDBinding.srlRecommend.finishLoadMore();

                if (dataX == null) return;

                if (mPage == 1) {
                    mRecommendAdp.setNewData(dataX.getData());
                } else {
                    mRecommendAdp.addAll(dataX.getData());
                }

                if (dataX.getTotal() % mSize > 0) {
                    totalPage = dataX.getTotal() / mSize + 1;
                } else {
                    totalPage = dataX.getTotal() / mSize;
                }

                if (mPage < totalPage) {
                    mVDBinding.srlRecommend.setEnableLoadMore(true);
                } else {
                    mVDBinding.srlRecommend.setEnableLoadMore(false);
                }
            }
        });
    }

    @Override
    protected void onVisibleAgain() {
        mVDBinding.srlRecommend.autoRefresh();
    }
}